package com.jdk.juc.tools;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLaunchTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(4);
        for(int i = 0; i < latch.getCount(); i++){
            new Thread( ()->{
                Random rand = new Random();
                int randomNum = rand.nextInt((3000 - 1000) + 1) + 1000;
                try {
                    TimeUnit.MILLISECONDS.sleep(randomNum);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" CountDownLatch Test "+((double)randomNum/1000)+"s");
                latch.countDown();
            }, "CountDownLatch Test "+i).start();
        }
        System.out.println("CountDownLatch start");
        latch.await();
        System.out.println("CountDownLatch end");
    }

}
